package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.dto.GetShopDto;
import com.woniuxy.dto.GoodBrandGeneralDto;
import com.woniuxy.dto.SaveShopDto;
import com.woniuxy.dto.WarehouseShopDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.*;
import com.woniuxy.entity.*;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.GoodsBrandException;
import com.woniuxy.exception.GoodsGeneralException;
import com.woniuxy.exception.PawnShopException;
import com.woniuxy.mapper.GoodsBrandMapper;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.AddGoodsBrandParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.DeleteBrandParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.ListGoodsBrandParam;
import com.woniuxy.param.goodsGeneralAndSmallAndBrand.UpdateGoodsBrandParam;
import com.woniuxy.service.GoodsBrandGeneralService;
import com.woniuxy.service.GoodsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.GoodsGeneralService;
import com.woniuxy.util.WoniuBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 * 商品品牌信息服务实现类
 */
@Service
public class GoodsBrandServiceImpl extends ServiceImpl<GoodsBrandMapper, GoodsBrand> implements GoodsBrandService {

    @Autowired
    private GoodsGeneralService goodsGeneralService;

    @Autowired
    private GoodsBrandGeneralService goodsBrandGeneralService;
    /**
     * 新增商品品牌信息
     *
     * @param addGoodsBrandParam
     * @return
     */
    @Override
    public GoodsBrandDto saveBrandInfo(AddGoodsBrandParam addGoodsBrandParam) {

        GoodsBrand brandByName = getOne(new QueryWrapper<GoodsBrand>().eq("goods_brand_name", addGoodsBrandParam.getGoodsBrandName()));

        if (null != brandByName)
            throw new GoodsBrandException(CodeEnum.BRAND_ADD_FAIL.getMessage(), CodeEnum.BRAND_ADD_FAIL.getCode());

        //满足新增条件 开始新增

        GoodsBrand goodsBrand = WoniuBeanUtils.copyObject(addGoodsBrandParam, GoodsBrand.class);
        boolean save = save(goodsBrand);
        if (save) {
            GoodsBrandDto goodsBrandDto = WoniuBeanUtils.copyObject(goodsBrand, GoodsBrandDto.class);
            ArrayList<GoodsBrandGeneral> goodsBrandGenerals = new ArrayList<>();
            for (Integer goodsGeneralId : addGoodsBrandParam.getGoodsGeneralIds()) {
                GoodsGeneral goodsGeneral = goodsGeneralService.getById(goodsGeneralId);
                GoodsBrandGeneral goodsBrandGeneral = new GoodsBrandGeneral();
                goodsBrandGeneral.setBrandId(goodsBrandDto.getGoodsBrandId());
                goodsBrandGeneral.setGeneralId(goodsGeneralId);
                goodsBrandGeneral.setGoodsGeneralName(goodsGeneral.getGoodsGeneralName());
                goodsBrandGenerals.add(goodsBrandGeneral);
            }
            boolean b = goodsBrandGeneralService.saveBatch(goodsBrandGenerals);
            if (b) {
                return goodsBrandDto;
            } else {
                throw new GoodsBrandException("大类添加失败",8001);
            }
        }
        throw new GoodsBrandException("添加失败",80002);
    }


    /**
     * 分页+模糊查询品牌信息
     *
     * @param limitParam
     * @return
     */
    @Override
    public PageGoodsBrandDto pageFind(ListGoodsBrandParam limitParam) {

        QueryWrapper<GoodsBrand> queryWrapper = new QueryWrapper<>();
        //0表示显示，才可以查询得到
        queryWrapper.eq("goods_brand_status", 0);
        if (null != limitParam.getKeyword()) {
            //模糊查询的条件应该是一个组合条件 即sql要加括号！
            queryWrapper.and(qw -> {
                qw.like("goods_brand_name", limitParam.getKeyword())
                        .or()
                        .like("goods_brand_simplename", limitParam.getKeyword());
            });
        }
        //加个按照主键降序的条件
        IPage<GoodsBrand> iPage = page(new Page<>(limitParam.getIndexPage(), limitParam.getPageSize()), queryWrapper);
        PageGoodsBrandDto pageGoodsBrandDto = new PageGoodsBrandDto();
        //塞入当前页
        pageGoodsBrandDto.setIndexPage(iPage.getCurrent());
        //塞入总条数
        pageGoodsBrandDto.setTotal(iPage.getTotal());
        //塞入数据
        pageGoodsBrandDto.setGoodsBrandDtos(WoniuBeanUtils.copyList(iPage.getRecords(), GoodsBrandDto.class));
        return pageGoodsBrandDto;
    }


    /**
     * 修改品牌信息
     *
     * @param updateGoodsBrandParam
     * @return
     */
    @Override
    public Boolean updateBrandInfo(UpdateGoodsBrandParam updateGoodsBrandParam) {
        GoodsBrand goodsBrand = new GoodsBrand();
        goodsBrand.setGoodsBrandId(updateGoodsBrandParam.getGoodsBrandId());
        goodsBrand.setGoodsBrandName(updateGoodsBrandParam.getGoodsBrandName());
        goodsBrand.setGoodsBrandSimplename(updateGoodsBrandParam.getGoodsBrandSimplename());
        goodsBrand.setGoodsBrandStatus(updateGoodsBrandParam.getGoodsBrandStatus());
        boolean b = updateById(goodsBrand);
        goodsBrandGeneralService.remove(new QueryWrapper<GoodsBrandGeneral>().eq("brand_id",updateGoodsBrandParam.getGoodsBrandId()));
        List<GoodsGeneral> goodsGenerals = goodsGeneralService.listByIds(updateGoodsBrandParam.getGoodsGeneralIds());
        ArrayList<GoodsBrandGeneral> goodsBrandGenerals = new ArrayList<>();
        for (GoodsGeneral goodsGeneral : goodsGenerals) {
            GoodsBrandGeneral goodsBrandGeneral = new GoodsBrandGeneral();
            goodsBrandGeneral.setBrandId(updateGoodsBrandParam.getGoodsBrandId());
            goodsBrandGeneral.setGeneralId(goodsGeneral.getGoodsGeneralId());
            goodsBrandGeneral.setGoodsGeneralName(goodsGeneral.getGoodsGeneralName());
            goodsBrandGenerals.add(goodsBrandGeneral);
        }
        boolean b1 = goodsBrandGeneralService.saveBatch(goodsBrandGenerals);
        if (!b1)
            throw new GoodsBrandException("大类存储失败",8005);
        return b;
    }


    /**
     * 批量删除品牌信息
     *
     * @param goodsBrandIds
     * @return 删除成功就返回true
     */
    @Override
    public Boolean deleteGoodsBrand(List<Integer> goodsBrandIds) {
        List<GoodsBrand> brandList = list(new QueryWrapper<GoodsBrand>().in("goods_brand_id", goodsBrandIds));
//        if (null == brandList || brandList.size() <= 0) throw new GoodsBrandException("品牌信息不存在", 1051);
        if (null == brandList || brandList.size() <= 0)
            throw new GoodsBrandException(CodeEnum.BRAND_INFO_NULL.getMessage(), CodeEnum.BRAND_INFO_NULL.getCode());

        //过滤掉不符合删除条件的数据
        List<GoodsBrand> collect = brandList.stream().filter(brand -> {
            Boolean flag = !(brand.getGoodsBrandStatus() == 1);
            //逻辑删除
            if (flag) brand.setGoodsBrandStatus(1);
            return flag;
        }).collect(Collectors.toList());//将符合删除条件的数据添加到新集合内
//        if (collect.size() != brandList.size()) throw new GoodsBrandException("有品牌不满足删除条件", 1011);
        if (collect.size() != brandList.size())
            throw new GoodsBrandException(CodeEnum.BRAND_DELETE_NOWAY.getMessage(), CodeEnum.BRAND_DELETE_NOWAY.getCode());

        return updateBatchById(collect);
    }

    @Override
    public GoodsBrandDto getBrandById(Integer id) {
        List<GoodBrandGeneralDto> goodBrandGeneralDtos = goodsBrandGeneralService.listByBrandId(id);
        List<GoodsBrandGeneral> goodsBrandGenerals = WoniuBeanUtils.copyList(goodBrandGeneralDtos, GoodsBrandGeneral.class);
        //存放此店铺下属仓库的id集合
        List<Integer> goodsGeneralIds=new ArrayList<>();

        for (GoodsBrandGeneral goodsBrandGeneral : goodsBrandGenerals) {
            goodsGeneralIds.add(goodsBrandGeneral.getGeneralId());
        }
        GoodsBrand byId = getById(id);
        GoodsBrandDto goodsBrandDto = WoniuBeanUtils.copyObject(byId, GoodsBrandDto.class);
        goodsBrandDto.setGoodsGeneralIds(goodsGeneralIds);
        return goodsBrandDto;
    }

}
